package com.tyl.lock8;

import java.util.concurrent.TimeUnit;

/***
 * 1、标准情况下：两个线程打印信息先发短信还是打电话？
 * 2、sendSms延迟四秒：是先发短信还是打电话？
 * */
public class Test1 {
    public static void main(String[] args) {
        Phone phone=new Phone();
        new Thread(()->{
            phone.sendSms();
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();
        new Thread(()->{
            phone.call();
        },"A").start();
    }
}

class  Phone{

    // synchronized 锁的对象是方法的调用者
    public  synchronized  void sendSms(){

        System.out.println("发短信");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
}